package simple.engine.managers;

public class ScreenManager {
	
	/* Screen */
	
	private static int mDesiredWidth;
	private static int mDesiredHeight;
	
	private static int mScaledWidth;
	private static int mScaledHeight;
	
	private static int mOffsetX = 0;
	private static int mOffsetY = 0;
	
	private static float mScale;
	
	// Getters and setters (static)
	
	public static float getScale()
	{
		return mScale;
	}
	
	public static int getOffsetX()
	{
		return mOffsetX;
	}
	
	public static int getOffsetY()
	{
		return mOffsetY;
	}

	public static int getScaledWidth() 
	{
		return mScaledWidth;
	}

	public static int getScaledHeight() 
	{
		return mScaledHeight;
	}
	
	// Methods (static)
	
	public static void setScreenConfig(float desiredWidth, float desiredHeight, float hardwareWidth, float hardwareHeight)
	{
		mDesiredWidth = (int) desiredWidth;
		mDesiredHeight = (int) desiredHeight;
		
		float desiredRatio = desiredWidth / desiredHeight;
		float hardwareRatio = hardwareWidth / hardwareHeight;
		
		if(desiredRatio < hardwareRatio) mScale = hardwareHeight / desiredHeight;
		else mScale = hardwareWidth / desiredWidth;
		
		mScaledWidth = (int) (desiredWidth * mScale);
		mScaledHeight = (int) (desiredHeight * mScale);
		
		mOffsetX = (int) (Math.abs(hardwareWidth - mScaledWidth)/2);
		mOffsetY = (int) (Math.abs(hardwareHeight - mScaledHeight)/2);
	}

	public static boolean isClickableArea(float x, float y) {
		if(mOffsetX < x && x < mOffsetX + mScaledWidth && mOffsetY < y && y < mOffsetY + mScaledHeight) return true;
		return false;
	}

	public static float[] getClickedWorldCoords(float touchX, float touchY, float CameraX, float CameraY)
	{
		return new float[]{ (touchX/mScale - mDesiredWidth/2 + CameraX - mOffsetX), (mOffsetY + CameraY - (touchY/mScale - mDesiredHeight/2))};
	}
	
}
